﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using Jy123.Web.AppCode;
using Jy123.Common;

namespace Jy123.Web.Manage
{
    public partial class Annexe : MgConfig
    {
        private Dictionary<int, Jy123.Model.Public_AnnexeType> dicAType = new Dictionary<int, Jy123.Model.Public_AnnexeType>();
        private Dictionary<int, Jy123.Model.Public_JType> dicJType = new Dictionary<int, Jy123.Model.Public_JType>();
        private Dictionary<int, Jy123.Model.Public_Level> dicLevel = new Dictionary<int, Jy123.Model.Public_Level>();
        protected void Page_Load(object sender, EventArgs e)
        {
            Verify("Annexe.aspx");
            Jy123.BLL.Public_AnnexeType BLLAType = new Jy123.BLL.Public_AnnexeType();
            Jy123.BLL.Public_JType BLLJType = new Jy123.BLL.Public_JType();
            Jy123.BLL.Public_Level BLLLevel = new Jy123.BLL.Public_Level();
            List<Jy123.Model.Public_AnnexeType> listAllAType = BLLAType.GetModelList(string.Empty);
            List<Jy123.Model.Public_JType> listAllJType = BLLJType.GetModelList(string.Empty);
            List<Jy123.Model.Public_Level> listAllLevel = BLLLevel.GetModelList(string.Empty);
            foreach (Jy123.Model.Public_AnnexeType aType in listAllAType)
            {
                dicAType.Add(aType.ID, aType);
            }
            foreach (Jy123.Model.Public_JType JType in listAllJType)
            {
                dicJType.Add(JType.ID, JType);
            }
            foreach (Jy123.Model.Public_Level level in listAllLevel)
            {
                dicLevel.Add(level.ID, level);
            }
            if (!Page.IsPostBack)
            {
                LoadInitialValue();
            }
        }

        protected void Repeater_DataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Jy123.Model.Public_Annexe annexe = e.Item.DataItem as Jy123.Model.Public_Annexe;
                HiddenField idHidden = e.Item.FindControl("idHidden") as HiddenField;
                idHidden.Value = annexe.ID.ToString();
                Label nameLabel = e.Item.FindControl("annexeNameLabel") as Label;
                Label aTypeLabel = e.Item.FindControl("annexeTypeLabel") as Label;
                Label JTypeLabel = e.Item.FindControl("JTypeLabel") as Label;
                Label levelLabel = e.Item.FindControl("levelLabel") as Label;
                Label earningsLabel = e.Item.FindControl("earningsLabel") as Label;
                Label raceEarningsLabel = e.Item.FindControl("raceEarningsLabel") as Label;
                Label xLabel = e.Item.FindControl("xLabel") as Label;
                Label yLabel = e.Item.FindControl("yLabel") as Label;
                Label priceLabel = e.Item.FindControl("priceLabel") as Label;
                Label displayOrderLabel = e.Item.FindControl("displayOrderLabel") as Label;
                nameLabel.Text = annexe.AName;
                aTypeLabel.Text = dicAType[annexe.FAType.Value].AName;
                JTypeLabel.Text = dicJType[annexe.FJType.Value].JName;
                if (annexe.Earnings.HasValue)
                {
                    earningsLabel.Text = (Convert.ToInt32(annexe.Earnings.Value)).ToString();
                }
                if (annexe.RaceEarnings.HasValue)
                {
                    raceEarningsLabel.Text = (Convert.ToInt32(annexe.RaceEarnings.Value)).ToString();
                }
                if (annexe.PosX.HasValue)
                {
                    xLabel.Text = annexe.PosX.Value.ToString();
                }
                if (annexe.PosY.HasValue)
                {
                    yLabel.Text = annexe.PosY.Value.ToString();
                }

                priceLabel.Text = (Convert.ToInt32(annexe.Price.Value)).ToString();
                displayOrderLabel.Text = annexe.DisplayOrder.Value.ToString();
                string strUL = annexe.UsingLevel;
                if (!string.IsNullOrEmpty(strUL))
                {
                    string strResult = string.Empty;
                    string[] strArray = strUL.Split(',');
                    for (int i = 0; i < strArray.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(strArray[i]))
                        {
                            int id = int.Parse(strArray[i]);
                            strResult += dicLevel[id].LName + ",";
                        }
                    }
                    if (strResult.Length > 1)
                    {
                        levelLabel.Text = strResult.Remove(strResult.LastIndexOf(","));
                    }
                }
            }
        }

        protected void Repeater_Command(object sender, RepeaterCommandEventArgs e)
        {
            RepeaterItem item = e.Item as RepeaterItem;
            HiddenField hidden = e.Item.FindControl("idHidden") as HiddenField;
            int id = int.Parse(hidden.Value);
            Jy123.BLL.Public_Annexe BLLAnnexe = new Jy123.BLL.Public_Annexe();
            if (e.CommandName.Equals("Edit"))
            {
                Jy123.Model.Public_Annexe annexe = BLLAnnexe.GetModel(id);
                actionTypeHidden.Value = "Update";
                idHidden.Value = id.ToString();
                JTypeDropDown.SelectedValue = annexe.FJType.Value.ToString();
                JTypeDropDown_Change(JTypeDropDown, EventArgs.Empty);
                ATypeDropDown.SelectedValue = annexe.FAType.Value.ToString();
                ATypeDropDown_Change(ATypeDropDown, EventArgs.Empty);
                annexeNameTextBox.Text = annexe.AName;
                if (annexe.Earnings.HasValue)
                {
                    earningsTextBox.Text = (Convert.ToInt32(annexe.Earnings.Value)).ToString();
                }
                if (annexe.RaceEarnings.HasValue)
                {
                    raceEarningsTextBox.Text = (Convert.ToInt32(annexe.RaceEarnings.Value)).ToString();
                }
                if (annexe.PosX.HasValue)
                {
                    xTextBox.Text = annexe.PosX.Value.ToString();
                }
                if (annexe.PosY.HasValue)
                {
                    yTextBox.Text = annexe.PosY.Value.ToString();
                }

                annexeSmallImage.Src = annexe.SmallModel;
                annexeBigImage.Src = annexe.Model;
                displayOrderTextBox.Text = annexe.DisplayOrder.Value.ToString();
                if (annexe.FAType.Value != Convert.ToInt32(Jy123.BLL.Public.EnumConfig.AnnexeType.AD))
                {
                    priceTextBox.Text = (Convert.ToInt32(annexe.Price.Value)).ToString();
                }
                else
                {
                    priceTextBox.Text = string.Empty;
                }
                desTextBox.Text = annexe.Description;
                if (annexe.FAType.Value == Convert.ToInt32(Jy123.BLL.Public.EnumConfig.AnnexeType.Billboard))
                {
                    adLevelTextBox.Text = annexe.ADLevel.Value.ToString();
                }
                else
                {
                    adLevelTextBox.Text = string.Empty;
                }
                if (annexe.FAType.Value == Convert.ToInt32(Jy123.BLL.Public.EnumConfig.AnnexeType.AD))
                {
                    adOrderTextBox.Text = annexe.AdOrder.Value.ToString();
                }
                else
                {
                    adOrderTextBox.Text = string.Empty;
                }
                string[] strLevel = annexe.UsingLevel.Split(',');
                foreach (ListItem clItem in ulCheckBoxList.Items)
                {
                    for (int i = 0; i < strLevel.Length; i++)
                    {
                        if (strLevel[i].Equals(clItem.Value))
                        {
                            clItem.Selected = true;
                        }
                    }
                }
            }
            else if (e.CommandName.Equals("Delete"))
            {
                BLLAnnexe.Delete(id);
                LoadInitialValue();
                ClearPage();
            }
        }

        protected void Save(object sender, EventArgs e)
        {
            Jy123.BLL.Public_Annexe BLLAnnexe = new Jy123.BLL.Public_Annexe();
            if (actionTypeHidden.Value.Equals("Add"))
            {
                Jy123.Model.Public_Annexe annexe = new Jy123.Model.Public_Annexe();
                SetAnnexe(annexe);
                int annexeId = BLLAnnexe.Add(annexe);
                annexe.SmallModel = CreateLogo(annexeId, CreateDir.ANNEXE, "SmallModel", fileUploadSmall.PostedFile);
                annexe.Model = CreateLogo(annexeId, CreateDir.ANNEXE, "Model", fileUploadBig.PostedFile);
                annexe.ID = annexeId;
                BLLAnnexe.Update(annexe);
            }
            else
            {
                int id = int.Parse(idHidden.Value);
                Jy123.Model.Public_Annexe annexe = BLLAnnexe.GetModel(id);
                SetAnnexe(annexe);
                string smallModel = CreateLogo(id, CreateDir.ANNEXE, "SmallModel", fileUploadSmall.PostedFile);
                string bigModel = CreateLogo(id, CreateDir.ANNEXE, "Model", fileUploadBig.PostedFile);
                if (!string.IsNullOrEmpty(smallModel))
                {
                    annexe.SmallModel = smallModel;
                }
                if (!string.IsNullOrEmpty(bigModel))
                {
                    annexe.Model = bigModel;
                }
                BLLAnnexe.Update(annexe);
            }
            LoadInitialValue();
            ClearPage();
        }

        private void SetAnnexe(Jy123.Model.Public_Annexe annexe)
        {
            if (JTypeDropDown.SelectedValue != "-1")
            {
                annexe.FJType = int.Parse(JTypeDropDown.SelectedValue);
            }
            if (ATypeDropDown.SelectedValue != "-1")
            {
                int id = int.Parse(ATypeDropDown.SelectedValue);
                annexe.FAType = id;
                switch (id)
                {
                    case 5:
                        //广告牌
                        annexe.Price = decimal.Parse(priceTextBox.Text);
                        annexe.Earnings = null;
                        annexe.RaceEarnings = null;
                        annexe.ADLevel = int.Parse(adLevelTextBox.Text);
                        annexe.AdOrder = null;
                        break;
                    case 6:
                        //广告内容
                        annexe.Price = 0;
                        annexe.Earnings = decimal.Parse(earningsTextBox.Text);
                        annexe.RaceEarnings = decimal.Parse(raceEarningsTextBox.Text);
                        annexe.ADLevel = null;
                        annexe.AdOrder = int.Parse(adOrderTextBox.Text);
                        break;
                    default:
                        annexe.Price = decimal.Parse(priceTextBox.Text);
                        annexe.Earnings = decimal.Parse(earningsTextBox.Text);
                        annexe.RaceEarnings = decimal.Parse(raceEarningsTextBox.Text);
                        annexe.ADLevel = null;
                        annexe.AdOrder = null;
                        break;
                }
            }
            if (!string.IsNullOrEmpty(annexeNameTextBox.Text))
            {
                annexe.AName = annexeNameTextBox.Text;
            }
            if (!string.IsNullOrEmpty(xTextBox.Text))
            {
                annexe.PosX = int.Parse(xTextBox.Text);
            }
            if (!string.IsNullOrEmpty(yTextBox.Text))
            {
                annexe.PosY = int.Parse(yTextBox.Text);
            }
            if (!string.IsNullOrEmpty(desTextBox.Text))
            {
                annexe.Description = desTextBox.Text;
            }
            if (!string.IsNullOrEmpty(displayOrderTextBox.Text))
            {
                annexe.DisplayOrder = int.Parse(displayOrderTextBox.Text);
            }
            string strLevel = ",";
            foreach (ListItem item in ulCheckBoxList.Items)
            {
                if (item.Selected)
                {
                    strLevel += item.Value + ",";
                }
            }
            if (strLevel.Length > 1)
            {
                annexe.UsingLevel = strLevel;
            }
            else
            {
                annexe.UsingLevel = string.Empty;
            }
        }

        private void ClearPage()
        {
            actionTypeHidden.Value = "Add";
            annexeNameTextBox.Text = string.Empty;
            earningsTextBox.Text = string.Empty;
            raceEarningsTextBox.Text = string.Empty;
            xTextBox.Text = string.Empty;
            yTextBox.Text = string.Empty;
            priceTextBox.Text = string.Empty;
            desTextBox.Text = string.Empty;
            adLevelTextBox.Text = string.Empty;
            adOrderTextBox.Text = string.Empty;
            earningUl.Visible = true;
            raceEarningUl.Visible = true;
            priceUl.Visible = true;
            adLevelUl.Visible = false;
            adOrderUl.Visible = false;
        }

        private void LoadInitialValue()
        {
            Jy123.BLL.Public_Annexe BLLAnnexe = new Jy123.BLL.Public_Annexe();
            List<Jy123.Model.Public_Annexe> listAnnexe = BLLAnnexe.GetModelList(string.Empty);
            annexeRepeater.DataSource = listAnnexe;
            annexeRepeater.DataBind();
            annexeRepeater.Visible = listAnnexe.Count > 0;
            BindDropDown();
        }

        private void BindDropDown()
        {
            Jy123.BLL.Public_AnnexeType BLLAType = new Jy123.BLL.Public_AnnexeType();
            Jy123.BLL.Public_JType BLLJType = new Jy123.BLL.Public_JType();
            List<Jy123.Model.Public_AnnexeType> listAllAType = BLLAType.GetModelList(string.Empty);
            List<Jy123.Model.Public_JType> listAllJType = BLLJType.GetModelList("JFather = 1 OR JFather = 3");
            ATypeDropDown.Items.Clear();
            ATypeDropDown.DataSource = listAllAType;
            ATypeDropDown.DataTextField = "AName";
            ATypeDropDown.DataValueField = "ID";
            ATypeDropDown.DataBind();
            ATypeDropDown.Items.Insert(0, new ListItem("请选择", "-1"));

            JTypeDropDown.Items.Clear();
            JTypeDropDown.DataSource = listAllJType;
            JTypeDropDown.DataTextField = "JName";
            JTypeDropDown.DataValueField = "ID";
            JTypeDropDown.DataBind();
            JTypeDropDown.Items.Insert(0, new ListItem("请选择", "-1"));
        }

        private void BindCheckBoxList(int id)
        {
            ulCheckBoxList.Items.Clear();
            ulDiv.Visible = id > 0;
            if (id > 0)
            {
                string strWhere = string.Format("FJType = {0}", id);
                Jy123.BLL.Public_Level BLLLevel = new Jy123.BLL.Public_Level();
                List<Jy123.Model.Public_Level> listLevel = BLLLevel.GetModelList(strWhere);
                ulCheckBoxList.DataSource = listLevel;
                ulCheckBoxList.DataTextField = "LName";
                ulCheckBoxList.DataValueField = "ID";
                ulCheckBoxList.DataBind();
            }
        }

        protected string CreateLogo(int annexeId, string type, string modelType, HttpPostedFile postFile)
        {
            CreateDir createDir = new CreateDir();
            string logoPath = postFile.FileName;
            string logoName = string.Empty;
            if (!string.IsNullOrEmpty(logoPath))
            {
                string logoType = logoPath.Substring(logoPath.LastIndexOf(".") + 1);
                logoName = createDir.CreateAnnexePath(type, annexeId, logoType, modelType);
                string mapPath = Server.MapPath("/");

                Upload upload = new Upload();
                upload.PostedFile = postFile;
                upload.FileLength = postFile.ContentLength;
                upload.FileName = logoName;
                upload.Extension = logoType;
                upload.SavePath = mapPath;
                upload.UploadStart(logoName);
            }
            return logoName;
        }

        protected void JTypeDropDown_Change(object sender, EventArgs e)
        {
            int id = int.Parse(JTypeDropDown.SelectedValue);
            BindCheckBoxList(id);
        }

        protected void ATypeDropDown_Change(object sender, EventArgs e)
        {
            int id = int.Parse(ATypeDropDown.SelectedValue);
            switch (id)
            {
                case 5:
                    //广告牌
                    earningUl.Visible = false;
                    raceEarningUl.Visible = false;
                    priceUl.Visible = true;
                    adLevelUl.Visible = true;
                    adOrderUl.Visible = false;
                    ulDiv.Visible = true;
                    break;
                case 6:
                    //广告内容
                    earningUl.Visible = true;
                    raceEarningUl.Visible = true;
                    priceUl.Visible = false;
                    adLevelUl.Visible = true;
                    adOrderUl.Visible = true;
                    ulDiv.Visible = false;
                    break;
                default:
                    earningUl.Visible = true;
                    raceEarningUl.Visible = true;
                    priceUl.Visible = true;
                    adLevelUl.Visible = false;
                    adOrderUl.Visible = false;
                    ulDiv.Visible = true;
                    break;
            }
        }
    }
}
